package com.feib.stms.batch.dao;


import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import org.springframework.stereotype.Repository;

import com.feib.stms.batch.dao.rowmapper.MessageQueueRowMapper;
import com.feib.stms.batch.domain.MessageQueue;





@Repository("messageQueueJdbcDao")
public class MessageQueueJdbcDao extends BaseJdbcDao {
	
	private OracleSequenceMaxValueIncrementer sequence;

	private SimpleJdbcInsert messageQueueJdbcInsert;
	
	
	@Override
	protected void initDao() throws Exception {
		super.initDao();
		this.sequence = new  OracleSequenceMaxValueIncrementer(this.getDataSource(), "MESSAGE_QUEUE_PK_SEQ");

		this.messageQueueJdbcInsert = new  SimpleJdbcInsert(this.getDataSource());
		this.messageQueueJdbcInsert.withTableName("MESSAGE_QUEUE")
		.usingColumns("MESSAGE_QUEUE_PK", "PAYMENT_PK", "FILE_FORMAT" ,"MQ_TYPE" ,"MQ_CONTEXT"  
				,"MQ_RESULT" ,"SEND_TIME", "RETURN_TIME", "RETURN_STATUS", "MESSAGE_ID"  );

	}
	
	public int insert(MessageQueue messageQueue) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		Long messageQueuePk = new Long(this.sequence.nextLongValue());
		parameters.put("MESSAGE_QUEUE_PK",  messageQueuePk);
		parameters.put("PAYMENT_PK",  messageQueue.getPaymentPk());
		parameters.put("FILE_FORMAT",  messageQueue.getFileFormat());
		parameters.put("MQ_TYPE",  messageQueue.getMqType());
		parameters.put("SEND_TIME",  messageQueue.getSendTime());
		parameters.put("MQ_CONTEXT",  messageQueue.getMqContext());
		parameters.put("MESSAGE_ID",  messageQueue.getMessageId());
		
		
		int insertCounts = this.messageQueueJdbcInsert.execute(parameters);
		if (insertCounts > 0)
			messageQueue.setMessageQueuePk(messageQueuePk);
		return insertCounts;

	}

	public int update(MessageQueue messageQueue) {

		Map<String, Object> parameters = new HashMap<String, Object>();

		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE MESSAGE_QUEUE " );
		sql.append(" SET MQ_RESULT = :MqResult ");
		sql.append(" ,RETURN_TIME = :ReturnTime ");
		sql.append(" ,RETURN_STATUS = :ReturnStaus ");
		sql.append(" WHERE MESSAGE_QUEUE_PK = :MessageQueuePk ");
		
		parameters.put("MqResult", messageQueue.getMqResult());
		parameters.put("ReturnTime", messageQueue.getReturnTime());
		parameters.put("ReturnStaus", messageQueue.getReturnStaus());
		parameters.put("MessageQueuePk", messageQueue.getMessageQueuePk());
		
		return this.getSimpleJdbcTemplate().update(sql.toString(), parameters);

	}
	
	public MessageQueue  getMessageQueueByPk(Long messageQueuePk) {
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append("SELECT * ");
		sql.append(" FROM MESSAGE_QUEUE M ");
		sql.append(" WHERE M.MESSAGE_QUEUE_PK= :MESSAGE_QUEUE_PK");
		parameters.put("MESSAGE_QUEUE_PK", messageQueuePk);
		
		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", parameters);


		return DataAccessUtils.singleResult(this.getSimpleJdbcTemplate().query(sql.toString(), new MessageQueueRowMapper(), parameters));

	}
	
	
	public MessageQueue  getMessageQueueByMessageId(String messageId) {
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append("SELECT * ");
		sql.append(" FROM MESSAGE_QUEUE M ");
		sql.append(" WHERE M.MESSAGE_ID= :MESSAGE_ID");
		parameters.put("MESSAGE_ID", messageId);
		
		logger.debug("SQL: {}", sql.toString());
		logger.debug("Args: {}", parameters);


		return DataAccessUtils.singleResult(this.getSimpleJdbcTemplate().query(sql.toString(), new MessageQueueRowMapper(), parameters));

	}


}
